# -*- codeing = utf-8 -*-
# @Time : 2021/2/22 22:40
# @Atuthor: 朱朱
# @File: 059字典的应用练习.py
# @Software: PyCharm
# chars = ['a', 'c', 'x', 'd', 'p', 'a', 'c', 'a', 'c', 'a']
#
# char_count = {}
#
# for char in chars:
#     if char not in char_count:
#         char_count[char]=chars.count(char)
# print(char_count)
#
# vs = char_count.values()
# print(vs)
# max = max(vs)
# print(max)
#
# for k,v in char_count.items():
#     if v == max:
#         print(k)

# ---------------------------------------------------

# persons = [
#     {'name': 'zhangsan', 'age': 18},
#     {'name': 'lisi', 'age': 20},
#     {'name': 'wangwu', 'age': 19},
#     {'name': 'jerry', 'age': 21}
# ]
# # 让用户输入姓名，如果姓名已经存在，提示用户;
# # 如果姓名不存在，继续输入年龄，并存入列表里
#
# un = input('请输入用户名:')
# for person in persons:
#     if person['name']== un:
#         print('您输入用户名已经存在')
#         break
# else: # 千万不能省略,有重要作用
#     new_person = {'name':un}
#     age = int(input('请输入你的年龄:'))
#     new_person['age']= age
#
#     persons.append(new_person)
#     print('用户添加成功')
#
# print(persons)
# ----------------------------------

# dict1 = {"a": 100, "b": 200, "c": 300}
# # dict2 = {}
# # for k,v in dict1.items():
# #     dict2[v] = k
# # print(dict2)
#
# dict2 = {k:v for k,v in dict1.items()}
# print(dict2)
# ----------------------------------------------------
# students = [
#     {'name': '张三', 'age': 18, 'score': 52, 'tel': '1388888998', 'gender': 'female'},
#     {'name': '李四', 'age': 28, 'score': 89, 'tel': '1388666666', 'gender': 'male'},
#     {'name': '王五', 'age': 21, 'score': 95, 'tel': '1365588889', 'gender': 'unknown'},
#     {'name': 'jerry', 'age': 20, 'score': 90, 'tel': '156666789', 'gender': 'unknown'},
#     {'name': 'chris', 'age': 17, 'score': 98, 'tel': '13777775523', 'gender': 'male'},
#     {'name': 'jack', 'age': 23, 'score': 52, 'tel': '13999999928', 'gender': 'female'},
#     {'name': 'tony', 'age': 15, 'score': 98, 'tel': '1388888888', 'gender': 'unknown'}
# ]
# (1) 统计不及格学生的个数
# (2) 打印不及格学生的名字和对应的成绩
# (3) 统计未成年学生的个数
# (4) 打印手机尾号是8的学生的名字
# (5) 打印最高分和对应的学生的名字
# count = 0
# nonage_count = 0
# max_score = students[0]['score']
# max_index = 0
# # print(max_score)
# for i,student in enumerate(students):
#     # print(i,student)
#     # print(student['score'])
#     if student['score']<60:
#         count+=1
#         print('不及格学生的名字:{},分数是{}'.format((student['name']),(student['score'])))
#     if student['age']<18:
#         nonage_count+=1
#     # if student['tel'].endswith('8'):
#     if student['tel'][-1] == '8':
#         print('手机尾号为8学生的名字:{},手机号是{}'.format((student['name']),(student['tel'])))
#     if student['score']>max_score:
#         max_score=student['score']
#         max_index = i # 这样有个弊端,求不到后面的最高分得主
# print('不及格学生的个数为:',count)
# print('未成年人数为:',nonage_count)
# print('最高成绩为{}:,学生名字为{}'.format(max_score, students[max_index]['name']))
# # 循环出所有最高得分者
# for student in students:
#     if student['score']== max_score:
#         print('最高分学生名字是',student['name'])

# (6) 删除性别不明的所有学生(这个地方有个坑，跳不出来的话大家可以在群里套路，或者等老师的解答)
# 方法一，将不需要删除的数据添加到新列表
# new_students = [x for x in students if x['gender']!='unknown']
# print(new_students)
# 方法二，使用for循环倒着删除要删除的数据，避免“坑”
# i = 0
# for i in range(len(students)-1,-1,-1):
#     if students[i]['gender']=='unknown':
#         students.remove(students[i])
# print(students)
# 方法三，使用while循环删除需要删除的数据，(这个不错)
# 并及时补齐因删除数据而导致的列表数据索引变化，避免漏删数据
# i = 0
# while i < len(students):
#     if students[i]['gender'] == 'unknown':
#         students.remove(students[i])
#         i-=1
#     i+=1
# print(students)
# 方法四，遍历在新的列表操作，删除是在原来的列表操作
# （students[:]是studens的切片，所以修改students对切片无影响）
# print(students[:])
# for student in students[:]:
#     if student['gender']=='unknown':
#         students.remove(student)
# print(students)
# 方法五，使用内建函数filter()和匿名函数
# new_students = filter(lambda x: x['gender']!='unknown',students)
# print(new_students) # <filter object at 0x0000023CD341C2C8>
# print(list(new_students))

# (7) 将列表按学生成绩从大到小排序(选做)
# for j in range(0, len(students) - 1): # 外层循环
#     for i in range(0, len(students) - 1 - j): # 内层比较优化-j
#         if students[i]['score'] < students[i + 1]['score']:
#             students[i], students[i + 1] = students[i + 1], students[i]
# print(students)

# ----------------------------------------------------------

# 用三个元组表示三门学科的选课学生姓名(一个学生可以同时选多门课)

sing = ('李白', '白居易', '李清照', '杜甫', '王昌龄', '王维', '孟浩然', '王安石')
dance = ('李商隐', '杜甫', '李白', '白居易', '岑参', '王昌龄')
rap = ('李清照', '刘禹锡', '岑参', '王昌龄', '苏轼', '王维', '李白')
# (1) 求选课学生总共有多少人
# 元组之间支持加法运算
# 使用集合set可以去重
total = set(sing+dance+rap)
print(total)
print(len(total))

# (2) 求只选了第一个学科的人的数量和对应的名字
sing_only=[]
for p in sing:
    if p not in dance and p not in rap:
        sing_only.append(p)
print('只选择了第一个学科的有{}人,名字是{}'.format(len(sing_only),sing_only))

# (3) 求只选了一门学科的学生的数量和对应的名字
# (4) 求只选了两门学科的学生的数量和对应的名字
# (5) 求只选了三门学生的学生的数量和对应的名字

p_dict = {}
all_persong = sing+dance+rap
for name in all_persong:
    if name not in p_dict:
        p_dict[name]=all_persong.count(name)
print(p_dict)

one_d =[]
two_d =[]
thr_d = []
for k,v in p_dict.items():
    if v==1:
        one_d.append(k)
    if v==2:
        two_d.append(k)
    if v==3:
        thr_d.append(k)

print('报了一门的有{}人,名字是{}'.format(len(one_d),one_d))
print('报了二门的有{}人,名字是{}'.format(len(two_d),two_d))
print('报了三门的有{}人,名字是{}'.format(len(thr_d),thr_d))
